package client;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.OutputStream;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.JTextComponent;

import communication.Message;
import communication.exception.EmptyInputException;

public class Gui {
	private static String appTitle="Airline agent";
	
	private JFrame frame;
	private JScrollPane scrollPaneResponse;
	private JScrollPane scrollPaneRequest;
	private JPanel panelButton;
	private JButton buttonSend;
	private JButton buttonHelp;
	private JTextArea textRequest;
	private JTextArea textResponse;
	private OutputStream outputStream;
	
	
	public Gui(){
		frame=new JFrame(appTitle);
		frame.setLayout(new BorderLayout());
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		
		textResponse=new JTextArea(20, 90);
		textResponse.setEditable(false);
		textResponse.setLineWrap(true);
		scrollPaneResponse=new JScrollPane(textResponse);
		scrollPaneResponse.setBorder(BorderFactory.createTitledBorder("Received"));
		
		textRequest=new JTextArea(1, 60);
		textRequest.setEditable(true);
		textRequest.setLineWrap(true);
		textRequest.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				if(KeyEvent.VK_ENTER==e.getKeyCode()){
					try {
						writeRequest(textRequest, outputStream);
					} catch (EmptyInputException e1) {
						writeResponse(e1.toString());
						e1.printStackTrace();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		scrollPaneRequest=new JScrollPane(textRequest);
		scrollPaneRequest.setBorder(BorderFactory.createTitledBorder("Input message here"));
		
		panelButton=new JPanel();
		panelButton.setLayout(new GridLayout(2, 1, 3, 3));
		
		buttonHelp=new JButton("Help");
		panelButton.add(buttonHelp);
		
		buttonSend=new JButton("Send");
		buttonSend.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					writeRequest(textRequest, outputStream);
				} catch (EmptyInputException e1) {
					writeResponse(e1.toString());
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		panelButton.add(buttonSend);
		
		frame.getContentPane().add(scrollPaneResponse, BorderLayout.NORTH);
		frame.getContentPane().add(scrollPaneRequest, BorderLayout.CENTER);
		frame.getContentPane().add(panelButton, BorderLayout.EAST);
		frame.pack();
		frame.setVisible(true);
	}
	
	public void exceptionFrame(Exception exception){
		JInternalFrame internalFrame=new JInternalFrame("Exception");
		internalFrame.toFront();
		internalFrame.pack();
		internalFrame.setVisible(true);
	}
	
	public void writeResponse(String string){
		textResponse.append(string);
		textResponse.append("\n");
	}
	
	public void writeRequest(JTextComponent jTextComponent, OutputStream outputStream) throws EmptyInputException, IOException{
		try {
			String string=jTextComponent.getText().trim();
			Message request=new Message();
			request.setCommand(string);
			if(string==null||"".equals(string))
				throw new EmptyInputException("Empty Input");
			else if(outputStream==null)
				throw new EmptyInputException("No connect!");
			else
				outputStream.write(Message.byteFromObj(request));
			} finally {
				jTextComponent.setText(null);
			}
	}
	
	public void setOutput(OutputStream outputStream) {
		this.outputStream=outputStream;
	}

}
